home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / wx / py / dispatcher.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  7KB  |  267 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
  5. __cvsid__ = '$Id: dispatcher.py 39667 2006-06-11 00:13:05Z RD $'
  6. __revision__ = '$Revision: 39667 $'[11:-2]
  7. import exceptions
  8. import types
  9. import weakref
  10.  
  11. class DispatcherError(exceptions.Exception):
  12.     
  13.     def __init__(self, args = None):
  14.         self.args = args
  15.  
  16.  
  17.  
  18. class Parameter:
  19.     
  20.     def __repr__(self):
  21.         return self.__class__.__name__
  22.  
  23.  
  24.  
  25. class Any(Parameter):
  26.     pass
  27.  
  28. Any = Any()
  29.  
  30. class Anonymous(Parameter):
  31.     pass
  32.  
  33. Anonymous = Anonymous()
  34. connections = { }
  35. senders = { }
  36. _boundMethods = weakref.WeakKeyDictionary()
  37.  
  38. def connect(receiver, signal = Any, sender = Any, weak = True):
  39.     if signal is None:
  40.         raise DispatcherError, 'signal cannot be None'
  41.     
  42.     if weak:
  43.         receiver = safeRef(receiver)
  44.     
  45.     senderkey = id(sender)
  46.     signals = { }
  47.     if connections.has_key(senderkey):
  48.         signals = connections[senderkey]
  49.     else:
  50.         connections[senderkey] = signals
  51.         if sender not in (None, Any):
  52.             
  53.             def remove(object, senderkey = senderkey):
  54.                 _removeSender(senderkey = senderkey)
  55.  
  56.             
  57.             try:
  58.                 weakSender = weakref.ref(sender, remove)
  59.                 senders[senderkey] = weakSender
  60.  
  61.         
  62.     receivers = []
  63.     if signals.has_key(signal):
  64.         receivers = signals[signal]
  65.     else:
  66.         signals[signal] = receivers
  67.     
  68.     try:
  69.         receivers.remove(receiver)
  70.     except ValueError:
  71.         pass
  72.  
  73.     receivers.append(receiver)
  74.  
  75.  
  76. def disconnect(receiver, signal = Any, sender = Any, weak = True):
  77.     if signal is None:
  78.         raise DispatcherError, 'signal cannot be None'
  79.     
  80.     if weak:
  81.         receiver = safeRef(receiver)
  82.     
  83.     senderkey = id(sender)
  84.     
  85.     try:
  86.         receivers = connections[senderkey][signal]
  87.     except KeyError:
  88.         raise DispatcherError, 'No receivers for signal %r from sender %s' % (signal, sender)
  89.  
  90.     
  91.     try:
  92.         receivers.remove(receiver)
  93.     except ValueError:
  94.         raise DispatcherError, 'No connection to receiver %s for signal %r from sender %s' % (receiver, signal, sender)
  95.  
  96.     _cleanupConnections(senderkey, signal)
  97.  
  98.  
  99. def send(signal, sender = Anonymous, **kwds):
  100.     senderkey = id(sender)
  101.     anykey = id(Any)
  102.     receivers = []
  103.     
  104.     try:
  105.         receivers.extend(connections[senderkey][signal])
  106.     except KeyError:
  107.         pass
  108.  
  109.     anyreceivers = []
  110.     
  111.     try:
  112.         anyreceivers = connections[senderkey][Any]
  113.     except KeyError:
  114.         pass
  115.  
  116.     for receiver in anyreceivers:
  117.         if receivers.count(receiver) == 0:
  118.             receivers.append(receiver)
  119.             continue
  120.     
  121.     anyreceivers = []
  122.     
  123.     try:
  124.         anyreceivers = connections[anykey][signal]
  125.     except KeyError:
  126.         pass
  127.  
  128.     for receiver in anyreceivers:
  129.         if receivers.count(receiver) == 0:
  130.             receivers.append(receiver)
  131.             continue
  132.     
  133.     anyreceivers = []
  134.     
  135.     try:
  136.         anyreceivers = connections[anykey][Any]
  137.     except KeyError:
  138.         pass
  139.  
  140.     for receiver in anyreceivers:
  141.         if receivers.count(receiver) == 0:
  142.             receivers.append(receiver)
  143.             continue
  144.     
  145.     responses = []
  146.     for receiver in receivers:
  147.         if type(receiver) is weakref.ReferenceType or isinstance(receiver, BoundMethodWeakref):
  148.             receiver = receiver()
  149.             if receiver is None:
  150.                 continue
  151.             
  152.         
  153.         response = _call(receiver, signal = signal, sender = sender, **kwds)
  154.         responses += [
  155.             (receiver, response)]
  156.     
  157.     return responses
  158.  
  159.  
  160. def _call(receiver, **kwds):
  161.     if hasattr(receiver, '__call__'):
  162.         if hasattr(receiver.__call__, 'im_func') or hasattr(receiver.__call__, 'im_code'):
  163.             receiver = receiver.__call__
  164.         
  165.     if hasattr(receiver, 'im_func'):
  166.         fc = receiver.im_func.func_code
  167.         acceptable = fc.co_varnames[1:fc.co_argcount]
  168.     elif hasattr(receiver, 'func_code'):
  169.         fc = receiver.func_code
  170.         acceptable = fc.co_varnames[0:fc.co_argcount]
  171.     else:
  172.         raise DispatcherError, 'Unknown receiver %s of type %s' % (receiver, type(receiver))
  173.     if not fc.co_flags & 8:
  174.         for arg in kwds.keys():
  175.             if arg not in acceptable:
  176.                 del kwds[arg]
  177.                 continue
  178.         
  179.     
  180.     return receiver(**kwds)
  181.  
  182.  
  183. def safeRef(object):
  184.     if hasattr(object, 'im_self'):
  185.         if object.im_self is not None:
  186.             selfkey = object.im_self
  187.             funckey = object.im_func
  188.             if not _boundMethods.has_key(selfkey):
  189.                 _boundMethods[selfkey] = weakref.WeakKeyDictionary()
  190.             
  191.             if not _boundMethods[selfkey].has_key(funckey):
  192.                 _boundMethods[selfkey][funckey] = BoundMethodWeakref(boundMethod = object)
  193.             
  194.             return _boundMethods[selfkey][funckey]
  195.         
  196.     
  197.     return weakref.ref(object, _removeReceiver)
  198.  
  199.  
  200. class BoundMethodWeakref:
  201.     
  202.     def __init__(self, boundMethod):
  203.         self.isDead = 0
  204.         
  205.         def remove(object, self = self):
  206.             self.isDead = 1
  207.             _removeReceiver(receiver = self)
  208.  
  209.         self.weakSelf = weakref.ref(boundMethod.im_self, remove)
  210.         self.weakFunc = weakref.ref(boundMethod.im_func, remove)
  211.  
  212.     
  213.     def __repr__(self):
  214.         return '<bound method weakref for %s.%s>' % (self.weakSelf, self.weakFunc)
  215.  
  216.     
  217.     def __call__(self):
  218.         if self.isDead:
  219.             return None
  220.         else:
  221.             object = self.weakSelf()
  222.             method = self.weakFunc().__name__
  223.             
  224.             try:
  225.                 return getattr(object, method)
  226.             except AttributeError:
  227.                 return None
  228.  
  229.  
  230.  
  231.  
  232. def _removeReceiver(receiver):
  233.     for senderkey in connections.keys():
  234.         for signal in connections[senderkey].keys():
  235.             receivers = connections[senderkey][signal]
  236.             
  237.             try:
  238.                 receivers.remove(receiver)
  239.             except:
  240.                 pass
  241.  
  242.             _cleanupConnections(senderkey, signal)
  243.         
  244.     
  245.  
  246.  
  247. def _cleanupConnections(senderkey, signal):
  248.     receivers = connections[senderkey][signal]
  249.     if not receivers:
  250.         signals = connections[senderkey]
  251.         del signals[signal]
  252.         if not signals:
  253.             _removeSender(senderkey)
  254.         
  255.     
  256.  
  257.  
  258. def _removeSender(senderkey):
  259.     del connections[senderkey]
  260.     
  261.     try:
  262.         del senders[senderkey]
  263.     except:
  264.         pass
  265.  
  266.  
  267.